﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">
    var mail_tree_setting = {
        check: { enable: true },
        data: {
            simpleData: { enable: true }
        },
        callback: {
            onCheck: get_selected_mail
        }
    };

    var appendixsetting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    var treeObj

    $(function() {
        var appendixNode = $("#appendixNode").val();
        $.fn.zTree.init($("#appendixZtree"), appendixsetting, eval(appendixNode));
        treeObj = $.fn.zTree.getZTreeObj("appendixZtree");
    })

    // 添加附件
    function addAppendix() {
        var nodes = treeObj.getSelectedNodes();
        var appendixName = "";
        var appendix = "";

        for (var index = 0; index < nodes.length; index++) {
            if (nodes[index].id == "appendix")
                continue;
            appendixName += nodes[index].name + " ";
            appendix += nodes[index].id + ";";
        }

        $("#appendixName").html(appendixName);
        $("#appendixHi").val(appendix);
        $("#appendix").fadeOut("slow");
    }

    // 显示附件窗口
    function showAppendix() {
        $("#appendix").fadeIn("slow");
    }

    // 隐藏附件窗口
    function cancel() {
        $("#appendix").fadeOut("slow");
    }

    $(function() {
        var options = {
            url: "/Message/MailSend",
            type: "POST",
            success: function(data) {
                hide_cover();
                if (data.indexOf("alert") > 10 | data.indexOf("alert") == -1) {
                    $("#workarea-3-7").html(data);
                }
            }
        }
        $("#frmMailSend").ajaxForm(options);
        init_mail_tree();
    })
    var tabs = null;
    function init_mail_tree() {
        $.ajax({
            url: "/Message/GetMailTree",
            type: "POST",
            success: function(mailNode) {
                $.fn.zTree.init($("#user_mail_tree"), mail_tree_setting, eval(mailNode));
            }
        });

        var user_id = $("#userId").val();
        //mytxl
        $.ajax({
            url: "/Message/GetMailTxlTree",
            type: "POST",
            data: { user_id: user_id, type: "我的通讯录" },
            success: function(mailNode) {
                $.fn.zTree.init($("#mytxl_mail_tree"), mail_tree_setting, eval(mailNode));
            }
        });
        //sharetxl
        $.ajax({
            url: "/Message/GetMailTxlTree",
            type: "POST",
            data: { user_id: "1", type: "共享通讯录" },
            success: function(mailNode) {
                $.fn.zTree.init($("#sharetxl_mail_tree"), mail_tree_setting, eval(mailNode));
            }
        });

        tabs = $("#tabs_txl").tabs();
    }

    function getsavedmailList() {
        // savedmaillist
        $.ajax({
            type: "POST",
            url: "/Message/GetEmails",
            success: function(data) {
                if (data != "") {
                    //                    $("div.ztree_body").children("div:eq(0)").html("选择邮箱");
                    $.fn.zTree.init($("#saved_mail_tree"), mail_saved_setting, eval(data));
                }
                else {
                    $("#saved_mail_tree").append("<li>您没有已保存邮件</li>");
                }
            }
        })
    }

    function get_selected_mail() {
        var selected_mail = "";

        var usertree = $.fn.zTree.getZTreeObj("user_mail_tree");
        var usernode = usertree.getCheckedNodes(true);
        var usernodeCount = usernode.length;
        for (var nodeIndex = 0; nodeIndex < usernodeCount; nodeIndex++) {
            var mail = usernode[nodeIndex].id;
            //alert(mail);
            if (mail.indexOf("@") != -1)
                selected_mail += usernode[nodeIndex].id + ";";
        }

        var mytxltree = $.fn.zTree.getZTreeObj("mytxl_mail_tree");
        var mytxlnode = mytxltree.getCheckedNodes(true);
        var mytxlnodeCount = mytxlnode.length;
        for (var nodeIndex = 0; nodeIndex < mytxlnodeCount; nodeIndex++) {
            var mail = mytxlnode[nodeIndex].id;
            //alert(mail);
            if (mail.indexOf("@") != -1)
                selected_mail += mytxlnode[nodeIndex].id + ";";
        }

        var sharetxltree = $.fn.zTree.getZTreeObj("sharetxl_mail_tree");
        var sharetxlnode = sharetxltree.getCheckedNodes(true);
        var sharetxlnodeCount = sharetxlnode.length;
        for (var nodeIndex = 0; nodeIndex < sharetxlnodeCount; nodeIndex++) {
            var mail = sharetxlnode[nodeIndex].id;
            //alert(mail);
            if (mail.indexOf("@") != -1)
                selected_mail += sharetxlnode[nodeIndex].id + ";";
        }

        $("#mailTo").val(selected_mail);
    }

    function onSubmit() {
        var mailTo = $("#mailTo").val();
        var subject = $("#subject").val();
        var text = $("#text").val();
        if (mailTo == "") {
            alert("请填写收件人邮箱地址");
            return false;
        } else if (subject == "") {
            alert("请填写主题");
            return false;
        } else if (text == "") {
            alert("请填写内容");
            return false;
        }


        var id = $("#email_id").val();
        show_cover();
        if (id != "") {
            $.ajax({
                type: "POST",
                url: "/Message/SendEmail",
                data: { "emailId": id }
            });
            $("#email_id").val("");
        }
        return true;
    }

    function GetMail(event, treeId, treeNode) {
        var id = treeNode.id;
        $.getJSON(
        "/Message/GetEmail",
        { "emailId": id },
        function(jsonData) {
            $("#from").val(jsonData.address);
            $("#mailTo").val(jsonData.receives);
            $("#subject").val(jsonData.title);
            $("#text").val(jsonData.context);
            $("#email_id").val(jsonData.id);
            $("#appendixName").html(jsonData.appname);
            $("#appendixHi").val(jsonData.appid);
        }
       )

        $("div.ztree_body").children("div:eq(0)").html("选择联系人");
        init_mail_tree();
    }

    var mail_saved_setting = {
        data: {
            simpleData: { enable: true }
        },
        callback: {
            onClick: GetMail
        }
    };

    //    function GetMailsSavedTree() {
    //        //        $.ajax({
    //        //            type: "POST",
    //        //            url: "/Message/GetEmails",
    //        //            success: function(data) {
    //        //                if (data != "") {
    //        //                    $("div.ztree_body").children("div:eq(0)").html("选择邮箱");
    //        //                    $.fn.zTree.init($("#user_mail_tree"), mail_saved_setting, eval(data));
    //        //                }
    //        //                else
    //        //                    alert("您未保存邮件");
    //        //            }
    //        //        })
    //    }

    function save_curr_mail() {
        var id = $("#email_id").val();
        var title = $("#subject").val();
        var address = $("#from").val();
        var receiver = $("#mailTo").val();
        var context = $("#text").val();
        var appendixname = $("#appendixName").html();
        var appendixid = $("#appendixHi").val();
        $.ajax({
            type: "POST",
            url: "/Message/SaveEmail",
            data: { "title": title, "context": context, "address": address, "receives": receiver, "emailId": id, "appname": appendixname, "appid": appendixid },
            success: function(data) {
                if (data == "Success")
                    alert("保存成功");
            }
        });
    }
</script>

<div style="width: 1100px;">
    <div class="leftLand" style="padding-left: 15px; width: 750px;">
        <%using (Html.BeginForm("MailSend", "Message", FormMethod.Post, new { id = "frmMailSend" }))
          { %>
        <%=Html.Hidden("email_id", "")%>
        <%=Html.Hidden("appendixHi", ViewData["attach_id"])%>
        <input class="btn_style" type="submit" value="发送" onclick="return onSubmit();" style="margin-top: 5px;" />
        <input class="btn_style" type="button" value="保存" onclick="save_curr_mail()" />
        <%--<input class="btn_style" type="button" value="查看保存邮件" onclick="GetMailsSavedTree()" />--%>
        <div style="margin-top: 30px;">
            <%--<p style="margin: 15px 0 5px 0;">
                发件人:<%=Html.TextBox("from",ViewData["from"], new { @style = "width:500px; margin-left:8px; border:solid 1px #6C92AD;" })%>
           
            </p>--%>
            <%=Html.Hidden("from")%>
            <%=Html.Hidden("userName", ViewData["userName"])%>
            <%=Html.Hidden("userId", ViewData["userId"])%>
            <%--<p style="margin: 5px 0;">
                密&nbsp;&nbsp;&nbsp;码:<%=Html.Password("password", ViewData["empsw"], new { @style = "width:662px; margin-left:6px; border:solid 1px #6C92AD;" })%>
                密&nbsp;&nbsp;&nbsp;码:
            </p>--%>
            <%=Html.Hidden("empsw")%>
            <p style="margin: 5px 0;">
                收件人:<%=Html.TextBox("mailTo", "", new { @style = "width:662px; margin-left:8px; border:solid 1px #6C92AD;" })%>
            </p>
            <p style="margin: 5px 0;">
                主&nbsp;&nbsp;&nbsp;题:<%=Html.TextBox("subject", ViewData["subject"], new { @style = "width:662px; margin-left:6px; border:solid 1px #6C92AD;" })%>
            </p>
            <p style="margin: 5px 0; height: 270px;">
                <span style="float: left;">内&nbsp;&nbsp;&nbsp;容:</span> <span style="float: left;">
                    <%var body = "";
                      if (ViewData["body"] != null)
                          body = ViewData["body"].ToString();
                    %>
                    <%=Html.TextArea("text", body, new { style = "width:660px;height: 250px; margin-left:6px; border:solid 1px #6C92AD;" })%></span>
            </p>
            <div style="margin: 5px 0; width: 720px;">
                <div style="width: 52px; margin-top: 2px; float: left; display: inline; position: relative;">
                    <!--增加行间距-->
                    附&nbsp;&nbsp;&nbsp;件:</div>
                <!--修改字的宽度50_55-->
                <div id="appendixName" style="margin-top: 3px; width: 662px; float: left; height: 20px;
                    display: inline; border: solid 1px #6C92AD;">
                    <%=ViewData["attach_name"]%>
                </div>
                <!--修改宽度520_700,增加外上边界-->
                <input class="btn_style" type="button" onclick="showAppendix();" style="margin-top: 5px;"
                    value="附件" /><!--修改外左边界0_55-->
                <div class="divBox" id="appendix" style="display: none;">
                    <div id="appendixZtree" class="ztree">
                    </div>
                    <input type="hidden" id="appendixNode" value="<%=ViewData["appendix"] %>" />
                    <input type="button" onclick="addAppendix();" value="确定" />
                    <input type="button" onclick="cancel()" value="取消" />
                </div>
            </div>
        </div>
        <%} %>
    </div>
    <div class="rightLand" style="margin: 15px 0 0 0;">
        <div class="ztree_body" style="height: 420px;">
            <div style="width: 230px;">
                选择联系人</div>
            <div id="tabs_txl">
                <ul>
                    <li><a href="#tabs-1" style="font-size: 11px">公司</a></li>
                    <li><a href="#tabs-2" style="font-size: 11px">我的</a></li>
                    <li><a href="#tabs-3" style="font-size: 11px">共享</a></li>
                    <li><a href="#tabs-4" style="font-size: 11px" onclick="getsavedmailList()">存储邮件</a></li>
                </ul>
                <div id="tabs-1">
                    <div id="div_tree1" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                        <ul id="user_mail_tree" class="ztree" style="height: 355px;">
                        </ul>
                    </div>
                </div>
                <div id="tabs-2">
                    <div id="div_tree2" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                        <ul id="mytxl_mail_tree" class="ztree" style="height: 355px;">
                        </ul>
                    </div>
                </div>
                <div id="tabs-3">
                    <div id="div_tree3" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                        <ul id="sharetxl_mail_tree" class="ztree" style="height: 355px;">
                        </ul>
                    </div>
                </div>
                <div id="tabs-4">
                    <div id="div_tree4" class="zTreeDemoBackground left" style="width: 230px; margin-left: -10px">
                        <ul id="saved_mail_tree" class="ztree" style="height: 355px;">
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
